package com.cat.greedyAlgorithm;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/minimum-number-of-operations-to-make-arrays-similar/
 * @create 2025/10/8 16:00
 * @since JDK17
 */

public class Solution56 {
    public long makeSimilar(int[] nums, int[] target) {
        long ans = 0;
        List<Integer> o1 = new ArrayList<>(), o2 = new ArrayList<>(), e1 = new ArrayList<>(), e2 = new ArrayList<>();
        for (int num : nums) {
            if (num % 2 == 0) {
                e1.add(num);
            } else {
                o1.add(num);
            }
        }
        for (int num : target) {
            if (num % 2 == 0) {
                e2.add(num);
            } else {
                o2.add(num);
            }
        }
        Collections.sort(o1);
        Collections.sort(o2);
        Collections.sort(e1);
        Collections.sort(e2);
        for (int i = 0; i < o1.size(); i++) {
            int num = o1.get(i) - o2.get(i);
            if (num > 0) {
                ans += num / 2;
            }
        }
        for (int i = 0; i < e1.size(); i++) {
            int num = e1.get(i) - e2.get(i);
            if (num > 0) {
                ans += num / 2;
            }
        }

        return ans;
    }
}
